#pragma once

#include "net_def.h"
#include "net_socket.h"

class UdpSocket : public NetSocket
{
public:
	UdpSocket(boost::asio::io_service &io_service);
	~UdpSocket(void);

	inline udp::socket& socket() { return m_socket; }
public:
	void Close();

	bool Bind(WORD port); 

	bool Run();

	bool SendTo(const char *ip, WORD port,
		const void *data, size_t size);

	bool SendTo(const udp::endpoint &endpoint,
		const void *data, size_t size);

protected:
	virtual void OnRecvFrom(const udp::endpoint &endpoint,
		const void *data, size_t size);

private:
	void HandleRecvFrom(
		const boost::system::error_code& error,
		size_t size);

	void HandleSendTo(
		const boost::system::error_code &error,
		const char *send_buffer);

private:
	static const size_t MAX_UDP_PACK_LEN = 4 * 1024; // 4k

	udp::socket m_socket;

	udp::endpoint m_recvfrom_endpoint;
	char m_data_buffer[MAX_UDP_PACK_LEN];
};
